package de.koller.worlders.engine.entity.mo.phsics;

import de.koller.oceanFramework.OceanException;
import de.koller.oceanFramework.math.Vec;
import de.koller.oceanFramework.tree.Mother;
import de.koller.worlders.WSea;
import de.koller.worlders.engine.entity.IAABB;
import de.koller.worlders.engine.entity.plattform.Plattform;
import de.koller.worlders.engine.entity.zone.Zone;
import de.koller.worlders.engine.tiles.Tile;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MOMovingC2 extends WSea {
	
	@Mother private PhysicsMO mo;
	
	private VelocityIterator velocityIterator;
	private CollisionIterator collisionIterator;
	private PositionIterator positionIterator;
	
	private final Vec way = new Vec();
	private final Set<IAABB> contacts = new HashSet<>();

	@Override
	protected void init() {
		super.init();
		
		velocityIterator = new VelocityIterator( mo );
		collisionIterator = new CollisionIterator( mo );
		positionIterator = new PositionIterator( mo );
	}
	
	public void velocityStep( long elapsedTime ) {
		velocityIterator.step( elapsedTime );
	}
	
	public void collisionStepX() {
		collisionIterator.step( true );
	}
	
	public void collisionStepY() {
		collisionIterator.step( false );
	}
	
	public void handleContacts() {
		Set<Zone> zones = new HashSet<>();
		List<Plattform> plattforms = new ArrayList<>();
		
		for( IAABB a : contacts ) {
			//Tile t = (Tile)a;
			assert a instanceof Tile == false;
			
			if( a instanceof Zone )
				zones.add( (Zone)a );
			else if( a instanceof Plattform )
				plattforms.add( (Plattform)a );
			else
				throw new OceanException( "unknown type" );
		}
		
		mo.zoneC.step( zones );
		mo.impulse( null, way , plattforms );
	}
	
	public void positionStepX() {
		positionIterator.step( true );
	}
	
	public void positionStepY() {
		positionIterator.step( false );
	}

	public Vec getWay() {
		return way;
	}

	public Set<IAABB> getContacts() {
		return contacts;
	}
	
}
